package com.guoxiang.controller;

import com.google.gson.Gson;
import com.guoxiang.pojo.MessageBean;
import com.guoxiang.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/selectAll")
    public ModelAndView selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf8");
        //获取前台页面发送的page和limit
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        int p=Integer.valueOf(page);
        int l=Integer.valueOf(limit);
        //构造json字符串
        List<MessageBean> allMessage = messageService.getALLMessage((p - 1) * l, l);
        Map hashMap = new HashMap();
        hashMap.put("code","0");
        hashMap.put("msg","");
        hashMap.put("count","1000");
        hashMap.put("data",allMessage);
        String jsonString= new Gson().toJson(hashMap);
        System.out.println(jsonString);
        //向前台发送json字符串
        response.getWriter().write(jsonString);
        return null;
    }
}